package com.excel.read;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


public class GridTable implements Serializable {
	public int rowIndex = -1;
	public int colIndex = -1;
	public int rows = -1;
	public int cols = -1;
	public String name = "";
	
	public String description;


	private String runsParam;

	public int pmbNo;
	

	private String category;
	
	//TODO
	public Set<ColumnCategory> set = new HashSet<ColumnCategory>();
	

	public String[][] ids = null;
	public String[][] data = null;
	
	
	public int getRowIndex() {
	    return rowIndex;
	}
	public void setRowIndex(int rowIndex) {
	    this.rowIndex = rowIndex;
	}
	public int getColIndex() {
	    return colIndex;
	}
	public void setColIndex(int colIndex) {
	    this.colIndex = colIndex;
	}
	public int getRows() {
	    return rows;
	}
	public void setRows(int rows) {
	    this.rows = rows;
	}
	public int getCols() {
	    return cols;
	}
	public void setCols(int cols) {
	    this.cols = cols;
	}

	public int getRunColIndex(String runName) {
		int colIndex = -1;
		for(int i=1; i<data[0].length-1; i++) {
			if(data[0][i].equals(runName)) {
				colIndex = i;
				break;
			}
		}
		return colIndex;
	}
	

	private  String formatNum(String num) {
		
		return num;
	}
	

	public String getRunIdsParam(Set<ColumnCategory> set) {
		Iterator iterator = set.iterator();
		String aParam = "";		
		ColumnCategory runCategory = null;
		if(iterator.hasNext()) {
			runCategory = (ColumnCategory)iterator.next();
			aParam = aParam + runCategory.getId().longValue();
		}
		while(iterator.hasNext()) {	
			runCategory = (ColumnCategory)iterator.next();
			aParam = aParam + "@" + runCategory.getId().longValue();
		}
		return aParam;
	
	}
	
	
	

	public void tranform() {
		int aCols = set.size();
		int aRows = 0;
		runsParam = getRunIdsParam(set);
		Iterator iterator = set.iterator();
		
		ColumnCategory runCategory = null;
		if(iterator.hasNext()) {
			runCategory = (ColumnCategory)iterator.next();
			this.category = runCategory.getTime();
			
			aRows = runCategory.getUnits().size();
			this.rows = aRows+1;
			this.cols = aCols+1;
			this.data = new String[this.rows][this.cols];
			this.ids = new String[this.rows][this.cols];
			
			this.data[0][0] = "��";
			this.ids[0][0] = "��";
			
			//����
			this.description = runCategory.getDescription();
			this.data[0][runCategory.getOrderNo().intValue()] = runCategory.getSubName();	
			this.ids[0][runCategory.getOrderNo().intValue()] = runCategory.getSubName();
			Set<UnitCell> units = runCategory.getUnits();
			Iterator unitIterator = units.iterator();
			while(unitIterator.hasNext()) {
				UnitCell unitScore = (UnitCell)unitIterator.next();
				this.data[unitScore.getOrderNo().intValue()][0] = unitScore.getUnitName();
				this.ids[unitScore.getOrderNo().intValue()][0] = unitScore.getUnitName();
				this.data[unitScore.getOrderNo().intValue()][runCategory.getOrderNo().intValue()] = formatNum(""+unitScore.getScore());
				this.ids[unitScore.getOrderNo().intValue()][runCategory.getOrderNo().intValue()] = ""+unitScore.getId().longValue();
			}
			
		}
		
		while(iterator.hasNext()) {			
			runCategory = (ColumnCategory)iterator.next();			
			this.data[0][runCategory.getOrderNo().intValue()] = runCategory.getSubName();			
			Set<UnitCell> units = runCategory.getUnits();
			Iterator unitIterator = units.iterator();
			while(unitIterator.hasNext()) {
				UnitCell unitScore = (UnitCell)unitIterator.next();
				this.data[unitScore.getOrderNo().intValue()][0] = unitScore.getUnitName();
				this.ids[unitScore.getOrderNo().intValue()][0] = unitScore.getUnitName();
				this.data[unitScore.getOrderNo().intValue()][runCategory.getOrderNo().intValue()] = formatNum(""+unitScore.getScore());
				this.ids[unitScore.getOrderNo().intValue()][runCategory.getOrderNo().intValue()]  = ""+unitScore.getId().longValue();
			}
		}
	}
	
	public int getPmbNo() {
		return pmbNo;
	}
	public void setPmbNo(int pmbNo) {
		this.pmbNo = pmbNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRunsParam() {
		return runsParam;
	}
	public void setRunsParam(String runsParam) {
		this.runsParam = runsParam;
	}
	public Set<ColumnCategory> getSet() {
		return set;
	}
	public void setSet(Set<ColumnCategory> set) {
		this.set = set;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String[][] getIds() {
		return ids;
	}
	public void setIds(String[][] ids) {
		this.ids = ids;
	}
	public String[][] getData() {
		return data;
	}
	public void setData(String[][] data) {
		this.data = data;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
}